package bean.control;

import java.util.List;

import bean.entity.Member;
import bean.entityField.MemberField;
import bean.enumeration.MemberState;
import bean.persistence.PersistenceHelper;

public class MemberMgr {
	@SuppressWarnings("unchecked")
	public static long addMember(String stuNum, String stuName, String pass) {
		PersistenceHelper pHelper = new PersistenceHelper();
		
		pHelper.openPersistence();
		pHelper.openQuery(Member.class);
		try {
			Object[] params = {stuNum};
			List<Member> memList = (List<Member>)pHelper.query(MemberField.STUNUM + " == stuNumParam", null, null, "String stuNumParam", params);
			
			if (memList.isEmpty()) {
				Member mem = new Member(null, stuNum, stuName, pass, MemberState.CONFIRM.ordinal());
				
				return pHelper.insert(mem).getKey().getId();
			}
			else
				return 0;
		} finally {
			pHelper.closeQuery();
			pHelper.closePersistence();
		}
	}
	
	@SuppressWarnings("unchecked")
	public static Member queryMemberByStuNum(String stuNum) {
		PersistenceHelper pHelper = new PersistenceHelper();
		
		pHelper.openPersistence();
		pHelper.openQuery(Member.class);
		try {
			Object[] params = {stuNum};
			List<Member> memList = (List<Member>)pHelper.query(MemberField.STUNUM + " == stuNumParam", null, null, "String stuNumParam", params);
			Member mem = memList.get(0);
			Member copyMem = new Member(mem.getKey(), mem.getStuNum(), mem.getName(), mem.getPassword(), mem.getState());
			
			return copyMem;
		} finally {
			pHelper.closeQuery();
			pHelper.closePersistence();
		}
	}
	
	public static void changeStateToNormal(long memId) {
		PersistenceHelper pHelper = new PersistenceHelper();
		
		pHelper.openPersistence();
		try {
			Member mem = (Member)pHelper.getById(Member.class, memId);
			mem.setState(MemberState.NORMAL.ordinal());
		} finally {
			pHelper.closePersistence();
		}
	}
	
	public static Member getMemberById(long memId) {
		PersistenceHelper pHelper = new PersistenceHelper();
		
		pHelper.openPersistence();
		try {
			Member mem = (Member)pHelper.getById(Member.class, memId);
			Member copyMem = new Member(mem.getKey(), mem.getStuNum(), mem.getName(), mem.getPassword(), mem.getState());
			
			return copyMem;
		} finally {
			pHelper.closePersistence();
		}
	}
	
	@SuppressWarnings("unchecked")
	public static Member queryMemberByStuNumAndPass(String stuNum, String pass) {
		PersistenceHelper pHelper = new PersistenceHelper();
		
		pHelper.openPersistence();
		pHelper.openQuery(Member.class);
		try {
			Object[] params = {stuNum, pass};
			List<Member> memList = (List<Member>)pHelper.query(MemberField.STUNUM + " == stuNumParam && " + MemberField.PASSWORD + " == passParam", null, null, "String stuNumParam, String passParam", params);
			
			if (memList.isEmpty())
				return null;
			else {
				Member mem = memList.get(0);
				Member copyMem = new Member(mem.getKey(), mem.getStuNum(), mem.getName(), mem.getPassword(), mem.getState());
				
				return copyMem;
			}	
		} finally {
			pHelper.closeQuery();
			pHelper.closePersistence();
		}
	}
}
